package cn.com.surker.test;

import cn.com.surker.enums.StateEnum;
import cn.com.surker.production.dao.ProductionInvoiceDao;
import cn.com.surker.production.entity.ProductionIndent;
import cn.com.surker.production.entity.ProductionIndentPump;
import cn.com.surker.production.entity.ProductionInvoice;
import cn.com.surker.production.service.ErpIndentService;
import cn.com.surker.production.service.IndentPumpService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 临时用的计算功能
 *
 * @author luotao
 * @date 2025/5/15
 */
@Slf4j
@Configuration
@EnableScheduling
public class TempFunc {

	@Resource
	private ProductionInvoiceDao invoiceDao;

	@Autowired
	private IndentPumpService indentPumpService;

	@Autowired
	private ErpIndentService indentService;

	/**
	 * 同步发货单的累计车次和方量
	 */
	// @Scheduled(cron = "22 45 16 * * ?")
	public void syncInvTotalStere() {
		try {
			List<ProductionInvoice> invoiceList = invoiceDao.findTotalStere();
			if (invoiceList != null && invoiceList.size() > 0) {
				for (int i = 0, s = invoiceList.size(); i < s; i++) {
					ProductionInvoice invoice = invoiceList.get(i);
					Integer totalCar = invoice.getTotalCar();
					Double totalStere = invoice.getTotalStere();
					if (totalStere == null) {
						invoice.setTotalStere(0d);
					}
					//	更新数据
					try {
						invoiceDao.updateById(invoice);
					} catch (Exception e) {
						e.printStackTrace();
						System.err.println();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.err.println("更新发货单累计方量完成");
	}

	/**
	 * 同步泵送设备
	 */
	// @Scheduled(cron = "01 22 11 * * ?")
	public void syncInvCast() {
		QueryWrapper<ProductionIndentPump> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		qw.isNull("cast_mode_id");
		List<ProductionIndentPump> list = indentPumpService.list(qw);
		for (int i = 0, s = list.size(); i < s; i++) {
			ProductionIndentPump indentPump = list.get(i);
			//	查询任务单的浇筑方式
			Long indentId = indentPump.getIndentId();
			ProductionIndent indent = indentService.getById(indentId);
			if (indent != null) {
				Long castModeId = indent.getCastModeId();
				indentPump.setCastModeId(castModeId);
				indentPumpService.updateById(indentPump);
			}
		}

		System.err.println("同步浇筑方式完成");
	}

	@Scheduled(cron = "01 10 01 * * ?")
	public void syncInvPump() {
		//	查询发货单的泵送设备
		List<ProductionInvoice> invoiceList = invoiceDao.findNoPumpList();
		if (invoiceList != null && invoiceList.size() > 0) {
			for (int i = 0, s = invoiceList.size(); i < s; i++) {
				try {
					//	查询发货单的任务单的磅
					ProductionInvoice invoice = invoiceList.get(i);
					@NotNull(message = "请选择任务单") Long indentId = invoice.getIndentId();
					Map<String, Object> indentPump = invoiceDao.findIndentPump(indentId);
					Object linkIdObj = indentPump.get("linkId");
					if (linkIdObj == null) {
						//	没有泵送设备,创建一条数据
						Long orgId = invoice.getOrgId();
						Object castModeIdObj = indentPump.get("castModeId");
						Long castModeId = Long.valueOf(castModeIdObj.toString());
						ProductionIndentPump pip = new ProductionIndentPump(orgId, castModeId, null, null, null);
						pip.setIndentId(indentId);
						//	保存数据
						indentPumpService.save(pip);
						linkIdObj = pip.getId();
					}
					Long linkId = Long.valueOf(linkIdObj.toString());
					//	设置发货单数据
					invoice.setLinkPumpId(linkId);
					invoiceDao.updateById(invoice);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		System.err.println("更新完成2");
	}

}
